package main

import (
	"fmt"
	"github.com/gojek/heimdall/v7"
	"github.com/gojek/heimdall/v7/httpclient"
	"io/ioutil"
	"net/http"
	"time"
)

func main() {
	timeout := 3000 * time.Millisecond

	// First set a backoff mechanism. Constant backoff increases the backoff at a constant rate
	backoffInterval := 2 * time.Millisecond
	// Define a maximum jitter interval. It must be more than 1*time.Millisecond
	maximumJitterInterval := 5 * time.Millisecond
	backoff := heimdall.NewConstantBackoff(backoffInterval, maximumJitterInterval)

	// Create a new retry mechanism with the backoff
	retrier := heimdall.NewRetrier(backoff)

	client := httpclient.NewClient(
		httpclient.WithHTTPTimeout(timeout),
		httpclient.WithRetrier(retrier),
		httpclient.WithRetryCount(4),
		)
	// Use the clients GET method to create and execute the request
	//res, err := client.Get("https://www.google.com.hk/?gws_rd=cr,ssl", nil)
	req, _ := http.NewRequest(http.MethodGet, "https://www.google.com.hk/?gws_rd=cr,ssl", nil)
	// Call the `Do` method, which has a similar interface to the `http.Do` method
	res, err := client.Do(req)
	if err != nil{
		panic(err)
	}
	// Heimdall returns the standard *http.Response object
	body, err := ioutil.ReadAll(res.Body)
	fmt.Println(string(body))
}
